package com.wing.set.controller;

import com.alibaba.fastjson.JSON;
import com.wing.common.utils.JsonResult;
import com.wing.redis.utils.redis.RedisHelperImpl;
import com.wing.set.entity.ChanjetSet;
import com.wing.set.service.ChanjetSetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 畅捷通-参数设置管理2
 *
 * @author xiayang
 */
@Validated
@Api(value = "畅捷通-参数设置管理", tags = "#/chanjetSet/chanjetSet")
@RestController("chanjetSetController")
@RequestMapping("chanjetSet/admin/chanjetSet")
@RequiredArgsConstructor
@Slf4j
public class ChanjetSetController {

    @Resource
    private ChanjetSetService chanjetSetService;

    @Resource
    private RedisHelperImpl redisHelper;

    @ApiOperation(value = "畅捷通-参数信息", notes = "详情")
    @GetMapping("/get")
    public JsonResult get() {
        ChanjetSet byId = chanjetSetService.getById(1L);
        return JsonResult.success(byId);
    }

    @ApiOperation(value = "畅捷通-参数信息", notes = "更新")
    @PutMapping("/update")
    public JsonResult update(ChanjetSet chanjetSet, HttpServletRequest request) {
        boolean updateById = chanjetSetService.updateById(chanjetSet);
        log.info("畅捷通-----------update---------------参数信息11111111");
        if (updateById) {
            // 获取请求域名
          /*  String domain = request.getHeader("Host");
            if (domain == null || domain.isEmpty()) {
                domain = request.getServerName();
            } else if (domain.contains(":")) { // 如果Host头部包含端口号
                domain = domain.substring(0, domain.indexOf(":")); // 移除端口号
            }
*/
            // 从请求属性中获取拦截器设置的域名
            String currentDomain = (String) request.getAttribute("currentDomain");
            if (currentDomain == null || currentDomain.isEmpty()) {
                return JsonResult.fail("域名信息缺失");
            }
            log.info("畅捷通-----------update---------------参数信息2222："+currentDomain);
            //写入redis缓存
            redisHelper.valuePut(currentDomain+"-appMsgSecret-", chanjetSet.getAppMsgSecret());
            log.info("畅捷通-----------update---------------参数信息333333："+chanjetSet.getAppMsgSecret());
           return JsonResult.success("修改成功！");
        }

        log.info("畅捷通-----------update---------------参数信息5555失败");
        return JsonResult.fail("修改失败！");
    }

}
